package com.beidasoft.web.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by LiZhenbin on 2015/11/26.
 */

public class DataCodeTable {
    /**
     * 将出生日期转化为年龄
     *
     * @param age
     * @return
     */
    public static Integer getAgeByString(String age) {
        SimpleDateFormat str = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        int result = 0;
        try {
            if (age != null) {
                String birth = age;
                String nowDay = str.format(date);
                int birthYear = Integer.parseInt(birth.substring(0, 4));
                int birthMonth = Integer.parseInt(birth.substring(5, 7));
                int birthDay = Integer.parseInt(birth.substring(8, 10));
                int nowDayYear = Integer.parseInt(nowDay.substring(0, 4));
                int nowDayMonth = Integer.parseInt(nowDay.substring(5, 7));
                int nowDayDay = Integer.parseInt(nowDay.substring(8, 10));
                result = nowDayYear - birthYear;
                if (birthMonth > nowDayMonth) {
                    result = result - 1;
                } else if (birthMonth == nowDayMonth) {
                    if (birthDay > nowDayDay) {
                        result = result - 1;
                    }
                }
            } else {
                result = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getBirthByPersonId(String str) {
        //年份
        String a = str.substring(6, 10);
        //月份
        String b = str.substring(10, 12);
        //天
        String c = str.substring(12, 14);
        return a + "-" + b + "-" + c;
    }

}
